<?php

namespace app\admin\model\parts;

use think\Model;
use Throwable;


/**
 * Approval
 */
class Approval extends Model
{
    // 表名
    protected $name = 'parts_approval';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;
    protected $updateTime = false;


    public function admin(): \think\model\relation\BelongsTo
    {
        return $this->belongsTo(\app\admin\model\Admin::class, 'admin_id', 'id');
    }
    // 更新审核记录
    public function SynchronizeAuditRecords($obj_id,$approval_type):bool
    {
        if($approval_type == 0 or $approval_type == 1)
        {
            $this->model = new \app\admin\model\parts\Info;
        }
        if($approval_type == 2 or $approval_type == 3)
        {
            $this->model = new \app\admin\model\parts\Entry();
        }
        $data = [];
        $data['id'] = $obj_id;
        $data['is_approval'] = 0;
        return $this->model->save($data);

    }
    // 删除通过的记录
    public function  DeleteRecord($approval_type,$obj_id):bool
    {
        if($approval_type == 0 or $approval_type == 1)
        {
            $this->model = new \app\admin\model\parts\Info;
        }
        if($approval_type == 2 or $approval_type == 3)
        {
            $this->model = new \app\admin\model\parts\Entry();
        }
        $this->model->startTrans();
        try {
            $res = $this->model->find($obj_id)->delete();
            $this->model->commit();
            return $res;
        } catch (Throwable $e) {
            $this->model->rollback();
            return false;
        }
    }

}